<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search extends MY_Controller {
  function __construct(){
    parent::__construct();
  }

  function index($start = 0){
    $_get = $this->input->get();
    if(empty($_get)){
      show_404();
    }
    $this->_assign("keyword",$_get["keyword"]);


    PageUtil::addVar("title","ผลการค้นหา ".$_get["keyword"]." - Truck.co.th ตลาดซื้อขายรถบรรทุกและเครื่องจักรกลหนักทุกชนิด");
    

    $this->_update_search($_get["keyword"]);

    $this->_assign("posts",$this->_search($_get,$start));
    $this->_fetch("index");
  }

  function _update_search($keyword = NULL){
    if(empty($keyword)){
      return;
    }
    $keyword = trim($keyword);
    $whereUpdate["where"]["keyword"] = $keyword;
    $checkResult = $this->searchModel->get($whereUpdate);
    if(!empty($checkResult)){
      foreach ($checkResult as $key => $value) {
        $whereUpdateExist["id"] = $value["id"];
        $whereUpdateExist["count"] = ($value["count"]+1);
        $this->searchModel->add($whereUpdateExist);
      }
    }else{
      $whereUpdateExist["keyword"] = $keyword;
      $whereUpdateExist["count"] = 1;
      $this->searchModel->add($whereUpdateExist);
    }
    return;
  }

  function _search($_get = NULL, $start = 0){
    $this->load->library('pagination');
    $this->load->model("post_model","postModel");
    $this->load->model("images_model","imagesModel");

    $wherePost["like"]["title"] = $_get["keyword"];
    $wherePost["or_like"]["content"] = $_get["keyword"];
    $wherePost["or_like"]["code"] = $_get["keyword"];

    $postList = $this->postModel->get($wherePost);

    if(!empty($postList)){
      $postIDList = array();
      foreach ($postList as $key => $value) {
        $postIDList[] = $value["id"];
      }
    }else{
      return null;
    }

    if(!empty($_get["province"]) AND $_get["province"] != 0){
      $this->load->model("postmeta_model","postmetaModel");
      $wherePostmeta["where"]["key"] = "province";
      $wherePostmeta["where"]["value"] = intval($_get["province"]);
      $wherePostmeta["group"] = "pos_id";
      
      $provinceLists = $this->postmetaModel->get($wherePostmeta);
      if(!empty($provinceLists)){
        $postIDListProvince = array();
        foreach ($provinceLists as $key => $value) {
          $postIDListProvince[] = $value["pos_id"];
        }
        $wherePostLast["where_in"]["id"] = array_unique(array_intersect($postIDListProvince,$postIDList));
      }
    }else{
      $wherePostLast["where_in"]["id"] = array_unique($postIDList);
    }

    if(((!empty($_get["province"]) AND $_get["province"] != 0) AND empty($postIDListProvince)) ){
      return null;
    }

    $postList = null;

    //pagination
    $config['prev_link'] = '&laquo;';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';

    $config['next_link'] = '&raquo;';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';

    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';

    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';

    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';

    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['base_url'] = base_url("search/");
    $config['total_rows'] = $this->postModel->count_rows($wherePostLast);
    $config['per_page'] = 20;
    $config['uri_segment'] = $this->uri->total_segments();
    $this->pagination->initialize($config);

    $wherePostLast['limit']['amount'] = $config['per_page'];
    if($start > 0){
      $wherePostLast['limit']['start'] = $start;
    }else{
      $wherePostLast['limit']['start'] = 0;
    }
    
    $postList = $this->postModel->get($wherePostLast);
    if(!empty($postList)){
      foreach ($postList as $key => $value) {
        $postList[$key]["images"] = $this->imagesModel->get(array("where"=>array("pos_id"=>$value["id"])));
        $postList[$key]["categories"] = array_reverse(Util::getCategoryInherit($value["cat_id"]));
        $postList[$key]["province"] = Util::getProvince($postList[$key]["province"]);
        $postList[$key]["meta"] = Util::getPostMeta($postList[$key]["id"]);
        $postList[$key]["type"] = Util::getType($postList[$key]["typ_id"]);
        $postList[$key]["comment_count"] = Util::getCommentCount($postList[$key]["id"]);
      }
    }
    return $postList;
  }
}